import 'package:dio/dio.dart';
import 'aunt_net_help.dart';

class ResponseInterceptors extends InterceptorsWrapper {
  @override
  onResponse(Response response) async {
    RequestOptions option = response.request;
    try {

      ///一般只需要处理200的情况，300、400、500保留错误信息，外层为http协议定义的响应码
      if (response.statusCode == 200 || response.statusCode == 201) {
        ///内层需要根据实际返回结构解析，一般会有code，data，msg字段
        String code = response.data["c"];
        if (code == "1") {
          return ResultData(data:response.data,code:1,statusCode: 200,
              headers: response.headers,isSuccess: true);
        } else {
          return ResultData(data:response.data,code:int.parse(code),statusCode: 200,
              headers: response.headers,isSuccess: false);
        }
      }
    } catch (e) {
      print(e.toString() + option.path);

      return ResultData(data:response.data,code:int.parse(response.data["c"]),statusCode: response.statusCode,
          headers: response.headers,isSuccess: false);
    }

    return ResultData(data:response.data,code:int.parse(response.data["c"]),statusCode: response.statusCode,
        headers: response.headers,isSuccess: false);
  }
}